package com.travel.server.manager.impl.command.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.data.QueryOperateTopRefundRequest;
import com.travel.server.manager.dto.response.data.QueryOperateTopRefundResponse;
import com.travel.server.manager.dto.response.data.QueryOperateTopRefundResponse.RefundItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.RefundDal;
import com.travel.server.manager.model.dto.data.TopRefundDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 19:14
 * @Desc:
 */
@Component
@Slf4j
public class QueryOperateTopRefundCommand extends AbstractCommand<QueryOperateTopRefundRequest, QueryOperateTopRefundResponse> {

    @Autowired
    private RefundDal refundDal;

    @Override
    protected void checkInput(QueryOperateTopRefundRequest request) {
        if(request.getNum() == null || request.getNum() <=0 ){
            throwInvalidParam();
        }
    }

    @Override
    public QueryOperateTopRefundResponse handle(QueryOperateTopRefundRequest request) {
        checkInput(request);

        Integer total = refundDal.selectTotalRefund();
        List<TopRefundDto> list = refundDal.selectTopRefund(request.getNum());

        List<RefundItem> refundList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(TopRefundDto dto : list){
                RefundItem item = new RefundItem();
                item.setOrderCharge(dto.getOrderCharge());
                item.setProject(dto.getName());
                item.setRatio(CommonUtil.getProportion(dto.getTotal(), total));
                item.setRefundAmount(CommonUtil.fenToYuan(dto.getAmount()).toString());
                item.setRefundOrder(dto.getTotal() + "");
                item.setRefundReason(refundDal.selectMaxReason(dto.getId()));
                refundList.add(item);
            }
        }

        QueryOperateTopRefundResponse response = new QueryOperateTopRefundResponse();
        response.setRefundList(refundList);
        return response;
    }
}
